package com.longge;

public class CompressString {

    public String compressString(String S) {
        if (S.length() == 0) return "";
        int count = 0;
        char temp = S.charAt(0);
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < S.length(); i++) {
            char c = S.charAt(i);
            if (c == temp) {
                count++;
            } else {
                res.append(temp).append(count);
                temp = c;
                count = 1;
            }
        }
        res.append(temp).append(count);
        return res.toString().length() >= S.length() ? S : res.toString();
    }
}
